/*
 * Copyright (c) Extreme Vision Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ATLAS_BASE_H
#define ATLAS_BASE_H

#include <dlfcn.h>
#include <fstream>
#include <iostream>
#include <mutex>

#include "model_handle.h"

#include "acl/acl.h"
#include "acl/ops/acl_dvpp.h"
#include "evdeploy/core/ev_common_data.h"
#include "version_config.h"

#define TRY_RELEASE_POINTER(val, func)                                                                                 \
    if (val != nullptr)                                                                                                \
    {                                                                                                                  \
        func(val);                                                                                                     \
        val = nullptr;                                                                                                 \
    }
#define TRY_DELETE_POINTER(val, func)                                                                                  \
    if (val != nullptr)                                                                                                \
    {                                                                                                                  \
        func val;                                                                                                      \
        val = nullptr;                                                                                                 \
    }

typedef aclError(ACL_MODEL_LOAD)(const void *, size_t, uint32_t *, void *, size_t, void *, size_t);

namespace ev
{

#define CREATE_INFER_INS(ENGINE)                                                                                       \
    extern "C" void *create_infer_ins() { return new ENGINE; }
#define DESTROY_INFER_INS(ENGINE)                                                                                      \
    extern "C" void destroy_infer_ins(void *ins)                                                                       \
    {                                                                                                                  \
        ENGINE *t_ins = (ENGINE *)ins;                                                                                 \
        delete t_ins;                                                                                                  \
    }

    class EVBaseAscendModel
    {
      public:
        EVBaseAscendModel() {}

        virtual ~EVBaseAscendModel() { ; }

        // load model and initization
        virtual EVStatus Init(const char *net, void **config) = 0;

        // relsease source
        virtual EVStatus UnInit() = 0;

        // run optimize, like tensorrt optimize
        virtual EVStatus OptimizeModel(const char *model_name) = 0;

        // run inference
        virtual EVStatus Infer(EVModelData *in, EVModelData *out) = 0;

        virtual EVStatus SetIO(const std::vector<std::string> &inputs, const std::vector<std::string> &outputs) = 0;

      protected:
        bool m_init_flag = false;
    };

    class EVAscendModel
    {
      public:
        EVAscendModel();

        virtual ~EVAscendModel();

      public:
        aclrtRunMode m_acl_run_mode;
        aclrtContext *m_acl_context = nullptr;
        aclrtStream m_acl_stream;
        aclmdlDesc *m_model_desc;
        uint32_t m_model_id = 0;
        uint32_t m_device_id = 0;

      protected:
        /**
         * @brief set atlas context
         * @param[in] context: atlas context
         * @return
         */
        void SetContext(void *context);
        /**
         * @brief load model, if model is in onnx format, it will convert it to om model
         * @param[in] model_name: model file name
         * @return
         */
        void LoadModel(const char *model_name);
        /**
         * @brief convert onnx format model to om engine, using Ascend atc tool
         * @param[in] input_model_name: onnx model file name(please use absulotue path)
         * @param[in] output_model_name: output model name, it's better to use the same name (without postfix) with
         * input_model_name
         * @param[in] cmd_str: atc tool command
         * @return
         */
        void ConvertEngine(const std::string &input_model_name, const std::string &output_model_name,
                           const std::string &cmd_str);

      private:
        ev::AtlasModelHandle m_model_handle;
    };

} // namespace ev
#endif
